//
//  FrontPageVC.swift
//  MM2
//
//  Created by 李成明 on 2021/12/31.
//

import UIKit

class FrontPageVC: MViewController {
    
    private let viewHeight = kScreenHeight - kStateAndNavH - kTabBarAndSafeH
    private var isfirstIn = true
    
    private lazy var followView: FollowView = {
        let view = FollowView(frame: CGRect(x: kScreenWidth, y: 0, width: kScreenWidth, height: viewHeight))
        view.fatherVC = self
        return view
    }()
    
    private lazy var discoverView: DiscoverView = {
        let view = DiscoverView(frame: CGRect(x: 0, y: 0, width: kScreenWidth, height: viewHeight))
        view.fatherVC = self
        return view
    }()
    
    private lazy var headerView:FrontPageHeaderView = {
        let view = FrontPageHeaderView.init(frame: CGRect(x: 0, y: kStateBarHeight, width: kScreenWidth, height: kNaviBarHeight))
        view.add { [weak self] _ in
            (self?.tabBarController as? MTabBarController)?.hidentabar()
            self?.navigationController?.pushViewController(PostDailyVC(), animated: true)
        }.search { _ in
            print("search")
        }.discover { [weak self] _ in
            UIView.animate(withDuration: 0.25) {
                self?.scrollView.contentOffset.x = kScreenWidth
            }
        }.follow { [weak self] _ in
            UIView.animate(withDuration: 0.25) {
                self?.scrollView.contentOffset.x = 0
            }
        }
        return view
    }()
    
    private lazy var scrollView: UIScrollView = {
        let view = UIScrollView(frame: CGRect(x: 0, y: kStateAndNavH, width: kScreenWidth, height: viewHeight))
        view.contentSize = CGSize(width: kScreenWidth*2, height: viewHeight)
        view.bounces = false
        view.isPagingEnabled = true
        view.delegate = self
        return view
    }()
    
    private lazy var noticeView: HomepageNoticeView = {
        let view = HomepageNoticeView(frame: self.view.bounds)
        view.isHidden = true
        return view
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
        followView.dailyList.beginRequest()
        discoverView.dailyList.beginRequest()
        requestNoticeList()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        (self.tabBarController as? MTabBarController)?.showTababr()
    }
}

extension FrontPageVC {
    private func setupUI() {
        view.addsubViews([scrollView, headerView, noticeView])
        
        scrollView.addsubViews([followView, discoverView])
    }
}


extension FrontPageVC: UIScrollViewDelegate {
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        headerView.scheduleValue = scrollView.contentOffset.x/kScreenWidth
    }
}

extension FrontPageVC {
    private func requestNoticeList() {
        MoyaNetwork.request(target: BaseApi.noticeList) { json, jsonStr in
            guard let model =  NoticeModel.deserialize(from: jsonStr) else { return }
            
            var contentStr = ""
            for (index, item) in model.data.enumerated() {
                contentStr += "\(index + 1).\(item.content)\n\n"
            }
            self.noticeView.valueStr = contentStr
            self.noticeView.isHidden = model.data.isEmpty
        }
    }
}


